var templateSettingMixin = function (templateType) {
  var data = {
    loading: false,
    hasDrag: false,
    templateConfig: null,
    customTemplate: false,
    type: templateType
  };

  var customTemplateType = '';
  switch (templateType) {
    case 'purchaseOrder':
      data.ordertPurchaseColumnDropdown = TemplateColumnConfigUtil.getTemplateColumn('orderPurchaseColumns');
      data.warehousePurchaseColumnDropdown = TemplateColumnConfigUtil.getTemplateColumn('warehousePurchaseColumns');
      customTemplateType = 2;
      break;
    case 'prePurchaseOrder':
      data.prePurchaseOrderColumnDropdown = TemplateColumnConfigUtil.getTemplateColumn('prePurchaseColumns');
      customTemplateType = 4;
      break;
    case 'warehouseInoutOrder':
      data.warehouseInoutOrderColumnDropdown = TemplateColumnConfigUtil.getTemplateColumn('warehouseInoutOrderColumns');
      data.productWarehouseInoutOrderColumnDropdown = TemplateColumnConfigUtil.getTemplateColumn('productWarehouseInoutOrderColumns');
      customTemplateType = 1;
      break;
    case 'warehouseInOrder':
      data.warehouseInOrderColumnDropdown = TemplateColumnConfigUtil.getTemplateColumn('warehouseInOrderColumns');
      data.productWarehouseInOrderColumnDropdown = TemplateColumnConfigUtil.getTemplateColumn('productWarehouseInOrderColumns');
      customTemplateType = 6;
      break;
    case 'outOrder':
      data.outOrderColumnDropdown = TemplateColumnConfigUtil.getTemplateColumn('outOrderColumns');
      customTemplateType = 3;
      break;
    case 'issueOrder':
      data.issueOrderColumnDropdown = TemplateColumnConfigUtil.getTemplateColumn('issueOrderColumns');
      customTemplateType = 5;
      break;
    case 'returnOrder':
      data.returnOrderColumnDropdown = TemplateColumnConfigUtil.getTemplateColumn('returnOrderColumns');
      break;
  }
  data.customTemplateType = customTemplateType;

  return {
    provide: function () {
      return {
        templSetting: this
      };
    },

    props: {
      user: Object
    },

    data: function () {
      return data;
    },

    created: function () {
      this.loading = true;
      var self = this;
      this.getCustomTemplate().then(function (printTempl) {
        var customTemplate = printTempl !== null;
        self.customTemplate = customTemplate;
        if (!customTemplate) {
          self.initConfig().then(function () {
            self.loading = false;
          });
        } else {
          self.loading = false;
        }
      }).catch(function () {
        self.loading = false;
      });
    },

    methods: {
      getCustomTemplate: function () {
        if (this.type === 'returnOrder') {
          return Promise.resolve(null);
        }

        var customTemplateType = this.customTemplateType;
        return ajaxRequest('printTemplate/getPrintDataTemplateByType', {
          type: customTemplateType
        }).then(function (templs) {
          var templ = templs[0];
          var printTempl;
          if (templ === undefined) {
            printTempl = null;
          } else {
            printTempl = templ.template;
            if (SysUtil.isEmpty(printTempl)) {
              printTempl = null;
            }
          }
          return printTempl;
        });
      },

      initConfig: function () {
        var self = this;
        return CommonTemplateUtil.getTableTemplateConfig(this.type).then(function (templateConfig) {
          self.templateConfig = templateConfig;
          return templateConfig;
        }).catch(function () {
          return null;
        });
      },

      saveTemplate: function () {
        var templateConfig = this.templateConfig;
        var validData = templateConfig.checkValid();
        if (validData !== '') {
          var msg = '';
          if (_.isArray(validData)) {
            var tableName = validData[0];
            tableName = i18n.getMsg(`templateSetting.tableName.${tableName}`);
            var index = validData[1];
            var columnName = validData[2];
            columnName = i18n.getMsg(`templateSetting.columnName.${columnName}`);
            msg = `${tableName}的第${index+1}列中的${columnName}未设置`;
          } else {
            msg = validData;
          }
          this.$alert(msg, '信息');
          return;
        }

        var template = templateConfig.getObject();
        this.toSaveTemplate(template);
      },

      toSaveTemplate: function (template) {
        var type = this.type;
        var name = i18n.getMsg(`templateSetting.type.${type}`);

        this.loading = true;
        var self = this;
        ajaxRequest('printTemplate/saveCommonOrderTemplate', {
          type: type,
          name: name,
          template: JSON.stringify(template)
        }).then(function (msg) {
          self.loading = false;
          self.$alert(msg, '信息');
        }).catch(function () {
          self.loading = false;
        });
      }
    },

    components: {
      OrderTemplateSetting: OrderTemplateSetting,
      TableColumnSetting: TableColumnSetting
    }
  };
};
